﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.SessionState;
using PMPUtils.Resources;

namespace PMP
{
    public class FormUtil
    {
        /// <summary>
        /// Authenticate on the form
        /// </summary>
        /// <param name="Session"></param>
        /// <param name="Response"></param>
        public static void Authenticate(HttpSessionState Session, HttpResponse Response)
        {
            if (Session["user_name"] == null)
            {
                // Log in first
                Response.Redirect("../login.aspx?MSG_ID=1");
            }

            String userName = Session["user_name"].ToString().Trim();

            if (userName.Equals(String.Empty))
            {
                // Log in first
                Response.Redirect("../login.aspx?MSG_ID=1");
            }
        }

        public static String GetSessionUserName(HttpSessionState Session)
        {
            String userName = null;
            if (Session["user_name"] != null && !Session["user_name"].ToString().Equals(String.Empty))
            {
                userName = Session["user_name"].ToString().Trim();
            }

            return userName;
        }

        /// <summary>
        /// Authenticate whether the log in is a lead
        /// </summary>
        /// <param name="Session"></param>
        /// <param name="Response"></param>
        public static void AuthenticateLead(HttpSessionState Session, HttpResponse Response)
        {
            if (Session["user_name"] == null)
            {
                // Log in first
                Response.Redirect("../login.aspx?MSG_ID=1");
            }

            String userName = Session["user_name"].ToString().Trim();

            if (userName.Equals(String.Empty))
            {
                // Log in first
                Response.Redirect("../login.aspx?MSG_ID=1");
            }

            if (!ResourceOperator.IsUserALead(userName))
            {
                // Access Denied
                Response.Redirect("../login.aspx?MSG_ID=2");
            }
        }
    }
}
